#!/usr/bin/python
# -*-coding: UTF-8 -*-
"""
# Time: 2021/11/19 23:12
# @Author: 修养
# @Project_name: PROJECT_NAME
"""
import pymysql
import cx_Oracle as ox


class Connect_Db:

    def __init__(self, host, user_name, password):
        self.host = host
        self.user_name = user_name
        self.password = password

    def connect_mysql(self):
        # 连接数据库
        conn = pymysql.connect(host=self.host, user=self.user_name, passwd=self.password, db='taobao')
        # 获取操作游标对象：相当于将所有数据存储起来，提供一个接口，通过游标对象操作接口里的数据，
        # 实现操作一条或多条数据，不然会将全部数据打印出来
        cursor = conn.cursor()
        # insert into emp (empno, ename,  job) values ('0002', '郭君儒', '校长')
        # 执行sql语句
        trump = ()
        try:
            # 执行sql语句
            # result = cursor.execute("insert into emp (empno, ename,  job) values ('0006', '谢若雪', '作家')")
            # print("执行条数：", result)
            # conn.commit()
            # 执行sql语句
            result_query = cursor.execute("select * from emp")
            # 返回查询数据
            trump = cursor.fetchall()
        except Exception:
            conn.rollback()
        finally:
            conn.close()
            return trump

    def connect_oracle(self):
        """
        连接Oracle数据库
        :return:
        """
        # 连接信息
        connect_info = '{user_name}/{password}@{host}:1521/orcl' \
            .format(user_name=self.user_name, password=self.password, host=self.host)
        print(connect_info)
        # 连接数据库
        conn = ox.connect(connect_info)
        # 获得游标对象
        cursor = conn.cursor()
        # users = ''
        # try:
        #     # 执行sql语句
        #     n = cursor.execute('select * from sm_users_tb where organ_no = {organ_no}'.format(organ_no=organ_no))
        #     users = cursor.fetchall()
        # except Exception:
        #     conn.rollback()
        # finally:
        #     conn.close()
        return cursor


if __name__ == '__main__':
    # 连接 mysql
    # db = connect_db('localhost', 'root', '')
    # lists = db.connect_mysql()
    # print(lists, type(lists))
    # 连接Oracle
    db = Connect_Db(user_name='sunaos', password='sunaos', host='localhost')
    result = db.connect_oracle(organ_no='5099')
    print(result)
    print(type(result))
